/*
Kakine
ActionScript Framework
Copyright © 2011, PUNY, LLC.

This file is part of KAKINE.

KAKINE is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

KAKINE is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with KAKINE.  If not, see <http://www.gnu.org/licenses/>.
*/ 
package com.puny.mvc.controllers
{
	import com.puny.mvc.models.EntityModels;
	import com.puny.mvc.models.Model;
	import com.puny.mvc.views.effect.SoundView;
	import com.puny.utils.Random;
	
	import flash.events.Event;

	public class VoiceController extends Controller
	{
		private static const LINE:String = "line";
		private static var _instance:VoiceController;
		private var _voices:Array;
		private var _playing:SoundView = null;
		private static var _config:XML;
		
		public static function getInstance():VoiceController {
			if (_instance == null) {
				_instance = new VoiceController(new _SingletonBlocker());
			}
			return _instance;
		}
		
		public static function set config(value:XML):void {
			_config = value;
		}
		
		public function VoiceController(singleton:_SingletonBlocker)
		{
			super();
		}
		
		public function get isPlaying():Boolean {
			return _playing != null;
		}
		
		override protected function setup():void {
			var line:XML
			var model:Model;
			var sound:SoundView;
			
			_voices = new Array();
			
			for each(line in _config.elements(LINE)) {
				sound = new SoundView(line.@url);
				_voices.push(sound);
				addChild(sound);
			}
			for each(var xml:XML in _config.elements()) {
				if (xml.name() != LINE) {
					trace(xml.name());
					model = EntityModels.getInstance(xml.name());
					model.rate = parseFloat(xml.@rate);
					model.lines = new Array();
					for each(line in xml.elements(LINE)) {
						trace("   ",line.@no);
						model.lines.push(parseInt(line.@no)-1);
					}
				}
			}
		}
			
		public function play(tag:String):void {
			var model:Model = EntityModels.getInstance(tag);
			var index:int;
			if (model.rate >= Math.random()&&!_playing) {
				index = Random.integer(model.lines.length);
				trace("voice id:"+model.lines[index]);
				_playing = _voices[model.lines[index]];
				_playing.addEventListener(SoundView.DONE, doneHandler, false, 0, true);
				_playing.play();
			}
		}
		
		public function stop():void {
			if (_playing != null) {
				_playing.stop();
			}
		}
		
		private function doneHandler(event:Event):void {
			event.currentTarget.removeEventListener(SoundView.DONE, doneHandler);
			_playing = null;
		}
			
		override public function afterClose():void {
			while(_voices.length > 0) {
				_voices.pop();
			}
		}	
	}
}

internal class _SingletonBlocker {}